package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.Teacher;
import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl implements UserDaoInterface {
    static private UserDaoInterface userDaoImpl = null;

    public UserDaoImpl() {}

    public static UserDaoInterface getInstance() {
        if (userDaoImpl == null) {
            userDaoImpl = new UserDaoImpl();
            System.out.println("Create UserDaoImpl Object");
        }
        return userDaoImpl;
    }

    @Override
    public User find(Long id) throws SQLException {
        // 创建 User 类型的引用，暂不创建对象
        User desiredUser = null;
        // 获得数据库连接
        Connection connection = JdbcHelper.getConn();
        String sql_select_id = "SELECT * FROM bysj.user WHERE id = ?";
        PreparedStatement pstmt_select_id = connection.prepareStatement(sql_select_id);
        pstmt_select_id.setLong(1, id);
        ResultSet resultSet_select_id = pstmt_select_id.executeQuery();
        // 如果表中存在 id 对应的记录，则获得表中的字段值，并创建对象
        if (resultSet_select_id.next()) {
            String no = resultSet_select_id.getString("no");
            String username = resultSet_select_id.getString("username");
            Long teacherId = resultSet_select_id.getLong("teacher_id");
            // 创建 Teacher 对象（假设 Teacher 类有相应的构造函数）
            Teacher teacher = new Teacher();
            teacher.setId(teacherId);
            desiredUser = new User(id, no, username, teacher);
        }
        // 关闭三个资源
        JdbcHelper.close(resultSet_select_id, pstmt_select_id, connection);
        return desiredUser;
    }

    @Override
    public boolean add(Connection connection, User user) throws SQLException {
        // 准备预编译的语句
        String sql_insert = "INSERT INTO user (no, username, teacher_id) VALUES (?, ?, ?)";
        PreparedStatement pstmt_add = connection.prepareStatement(sql_insert);

        pstmt_add.setString(1, user.getNo());
        pstmt_add.setString(2, user.getUsername());
        pstmt_add.setLong(3, user.getTeacher().getId());

        // 执行预编译语句，影响的行数保存在 rowAffected 中
        int rowAffected = pstmt_add.executeUpdate();
        return rowAffected > 0;
    }

    @Override
    public boolean delete(Connection connection, Long id) throws SQLException {
        String sql_delete = "DELETE FROM user WHERE id = ?";
        PreparedStatement pstmt_delete = connection.prepareStatement(sql_delete);
        pstmt_delete.setLong(1, id);
        int rowAffected = pstmt_delete.executeUpdate();
        return rowAffected > 0;
    }

    @Override
    public void login(Connection connection, User user) throws SQLException {
        String sql_login = "SELECT * FROM user WHERE username = ? and password = ?";
        PreparedStatement pstmt_login = connection.prepareStatement(sql_login);
        pstmt_login.setString(1,user.getUsername());
        pstmt_login.setString(2,user.getPassword());
        ResultSet resultSet_login = pstmt_login.executeQuery();
        if (resultSet_login.next()){
            Long user_id = resultSet_login.getLong("id");
            user.setId(user_id);
        }
    }
}